package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.FxyFinancialAccountingCategoryDetailsMapper;
import com.ruoyi.system.domain.FxyFinancialAccountingCategoryDetails;
import com.ruoyi.system.service.IFxyFinancialAccountingCategoryDetailsService;

/**
 * 辅助核算类别明细列Service业务层处理
 * 
 * @author coco
 * @date 2025-02-25
 */
@Service
public class FxyFinancialAccountingCategoryDetailsServiceImpl implements IFxyFinancialAccountingCategoryDetailsService 
{
    @Autowired
    private FxyFinancialAccountingCategoryDetailsMapper fxyFinancialAccountingCategoryDetailsMapper;

    /**
     * 查询辅助核算类别明细列
     * 
     * @param id 辅助核算类别明细列主键
     * @return 辅助核算类别明细列
     */
    @Override
    @Transactional
    public FxyFinancialAccountingCategoryDetails selectFxyFinancialAccountingCategoryDetailsById(Long id)
    {
        return fxyFinancialAccountingCategoryDetailsMapper.selectFxyFinancialAccountingCategoryDetailsById(id);
    }

    /**
     * 查询辅助核算类别明细列列表
     * 
     * @param fxyFinancialAccountingCategoryDetails 辅助核算类别明细列
     * @return 辅助核算类别明细列
     */
    @Override
    @Transactional
    public List<FxyFinancialAccountingCategoryDetails> selectFxyFinancialAccountingCategoryDetailsList(FxyFinancialAccountingCategoryDetails fxyFinancialAccountingCategoryDetails)
    {
        return fxyFinancialAccountingCategoryDetailsMapper.selectFxyFinancialAccountingCategoryDetailsList(fxyFinancialAccountingCategoryDetails);
    }

    /**
     * 新增辅助核算类别明细列
     * 
     * @param fxyFinancialAccountingCategoryDetails 辅助核算类别明细列
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFxyFinancialAccountingCategoryDetails(FxyFinancialAccountingCategoryDetails fxyFinancialAccountingCategoryDetails)
    {

//        String name = fxyFinancialAccountingCategoryDetails.getName();

        // TODO 保存的时候判断名称是否重复，不重复添加进常用表中
//        if (StringUtils.isNull(fxyFinancialAccountingCategoryDetails.getId())){
//            // 新增名称，
//        }

        fxyFinancialAccountingCategoryDetails.setCreateBy(SecurityUtils.getUsername());


        return fxyFinancialAccountingCategoryDetailsMapper.insertFxyFinancialAccountingCategoryDetails(fxyFinancialAccountingCategoryDetails);
    }

    /**
     * 修改辅助核算类别明细列
     * 
     * @param fxyFinancialAccountingCategoryDetails 辅助核算类别明细列
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFxyFinancialAccountingCategoryDetails(FxyFinancialAccountingCategoryDetails fxyFinancialAccountingCategoryDetails)
    {
        return fxyFinancialAccountingCategoryDetailsMapper.updateFxyFinancialAccountingCategoryDetails(fxyFinancialAccountingCategoryDetails);
    }

    /**
     * 批量删除辅助核算类别明细列
     * 
     * @param ids 需要删除的辅助核算类别明细列主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFxyFinancialAccountingCategoryDetailsByIds(Long[] ids)
    {
        return fxyFinancialAccountingCategoryDetailsMapper.deleteFxyFinancialAccountingCategoryDetailsByIds(ids);
    }

    /**
     * 删除辅助核算类别明细列信息
     * 
     * @param id 辅助核算类别明细列主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteFxyFinancialAccountingCategoryDetailsById(Long id)
    {
        return fxyFinancialAccountingCategoryDetailsMapper.deleteFxyFinancialAccountingCategoryDetailsById(id);
    }

    @Override
    public String importFxyFinancialAccountingCategoryDetails(List<FxyFinancialAccountingCategoryDetails> list, boolean b) {
        return null;
    }

    @Override
    @Transactional
    public String importUser(List<FxyFinancialAccountingCategoryDetails> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入供应商数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FxyFinancialAccountingCategoryDetails user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                FxyFinancialAccountingCategoryDetails u = fxyFinancialAccountingCategoryDetailsMapper.selectFxyFinancialAccountingCategoryDetailsById(user.getId());
                if (StringUtils.isNull(u))
                {
//                    BeanValidators.validateWithException(validator, user);
//                    deptService.checkDeptDataScope(user.getDeptId());
//                    String password = configService.selectConfigByKey("sys.user.initPassword");
//                    user.setPassword(SecurityUtils.encryptPassword(password));
//                    user.setCreateBy(operName);
                    fxyFinancialAccountingCategoryDetailsMapper.insertFxyFinancialAccountingCategoryDetails(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 导入成功");
                }
                else if (updateSupport)
                {
//                    BeanValidators.validateWithException(validator, user);
//                    checkUserAllowed(u);
//                    checkUserDataScope(u.getUserId());
//                    deptService.checkDeptDataScope(user.getDeptId());
//                    user.setUserId(u.getUserId());
                    user.setUpdateBy(operName);
                    fxyFinancialAccountingCategoryDetailsMapper.updateFxyFinancialAccountingCategoryDetails(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
